#define  _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    int maxSubarrayLength(vector<int>& nums, int k) {
        unordered_map<int, int> hash;
        int l = 0, r = 0;
        int n = nums.size();
        int ret = 0;
        while (r < n)
        {
            hash[nums[r]]++;
            while (hash[nums[r]] > k)
            {
                hash[nums[l]]--;
                l++;
            }
            r++;
            ret = max(ret, r - l);
        }
        return ret;
    }
};